package com.pan.service.impl;

import com.pan.dao.categoryHistory.CategoryHistoryDao;
import com.pan.dao.categoryHistory.OperationCategoryHistorySelectDao;
import com.pan.entity.OperationCategoryHistory;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.excep.error.ServiceException;
import com.pan.service.CategoryHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service("categoryHistoryService")
public class CategoryHistoryServiceImpl implements CategoryHistoryService {


    @Resource
    private OperationCategoryHistorySelectDao categoryHistorySelectDao;

    @Resource
    private CategoryHistoryDao categoryHistoryDao;


    @Override
    public Result<List<OperationCategoryHistory>> select(String adminTicket,Object selectValue,String selectName){
        if (selectName.equals("updateTime")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Long startTime = sdf.parse((String) selectValue).getTime();
                Long endTime = startTime + (24*60*60*1000);
                List<OperationCategoryHistory> li =  categoryHistoryDao.select();
                li = li.stream().filter(i->i.getUpdateTime().getTime()>startTime&&i.getUpdateTime().getTime()<endTime?true:false).collect(Collectors.toList());
                return new Result<>(ResultEnum.SUCCESS,li);
            } catch (ParseException e) {
                throw new ServiceException(403,"日期格式错误，应为yyyy-MM-dd");
            }
        }
        String[] memberNames = {"adminId","operation","categoryId","categoryName"};
        Boolean isNotNull = Arrays.stream(memberNames).filter(name ->name.equals("adminId")).findAny().isPresent();
        if (!isNotNull){
            return new Result(ResultEnum.PARAMETER_ERROR);
        }
        String methodName = "selectBy"+selectName.substring(0,1).toUpperCase()+selectName.substring(1);
        List<OperationCategoryHistory> li = null;
        try {
            Class selectType = OperationCategoryHistorySelectDao.class.getDeclaredField(selectName).getType();
            Method method = OperationCategoryHistorySelectDao.class.getMethod(methodName,selectType);
            li = (List<OperationCategoryHistory>) method.invoke(categoryHistorySelectDao,selectValue);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        Result result = new Result<>(ResultEnum.SUCCESS,li);
        return result;
    }


}
